"use strict";
/* Controllers */
app.controller("dataMetricDapanListController", ['$scope', '$location', '$rootScope', '$routeParams','dataMetricDapanListService', function ($scope, $location, $rootScope, $routeParams,dataMetricDapanListService) {



    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.biz = $routeParams.biz;
//    vm.p.dt = getDateStr(-30);
    vm.p.bizTime = getDateStr(-1);

    //vm.biz = "zp";// [zp,ganji,chinahr]
    vm.result = {};
    vm.items=[];


    var datePicker = $('.date');
    datePicker.datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD'
    });
    datePicker.on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input');
        var eName = currentInput.attr('name');
        var p = $scope.vm.p;
        p[eName] = currentInput.val();
        $scope.$apply()
    });

    if ($scope.vm.p.bizTime == '' || typeof ($scope.vm.p.bizTime) == 'undefined') {
        $scope.vm.p.bizTime = new Date(new Date().getTime() - 86400000).format('yyyy-MM-dd');
    }

    var errorFn = function (result) {
        alert(result);
    };
    $scope.listZhibiaoDapan = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 查询最近30天的数据
        var params = {
            dt:getDateStr(-30),
            dt2:vm.p.bizTime,
            biz: vm.p.biz
        };

        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            //$('#list_table').bootstrapTable('load', data);
            vm.items=[];
            data.forEach(function(item,index){
                var arr = [];
                var series = item.series;
                var rows = series.split(',');
                for(var i=0;i<rows.length; ++i) {
                    var kv = rows[i].split('^');
                    var v = 0;
                    if(kv.length==2) {
                        v = Number(kv[1]);
                    }
                    arr.push([kv[0].trim(),v]);
                }
                // 进行快速排序
                arr = quick_sort(arr);

                item.numbs = arr;
                vm.items.push(item);
            });
            //vm.ready = true;
        };
        dataMetricDapanListService.listZhibiaoDapan(params, procFn, errorFn);
    };

    $scope.renderFinish = function(){
        setTimeout(function(){
            sparkLine();
        },100);
    };

    $scope.queryReport = function() {
        $scope.listZhibiaoDapan();
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [10, 30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.listZhibiaoDapan();
    };
    $scope.initializeController();

}]);

function quick_sort(arr) {
    if (arr.length <= 1) {
        return arr
    }
    var quickIndex = Math.floor(arr.length / 2)
    var mid = arr.splice(quickIndex, 1)[0]
    var left = []
    var right = []
    for (var i = 0; i < arr.length; i++) {
        if (arr[i][0] > mid[0]) {
            left.push(arr[i])
        } else {
            right.push(arr[i])
        }
    }
    return quick_sort(right).concat([mid], quick_sort(left))
}

/* Services */
app.service('dataMetricDapanListService', ['ajaxService', function (ajaxService) {

    this.listZhibiaoDapan = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/dapan/listDapanSeriesAll.do", successFunction, errorFunction);
    };
}]);

app.filter("topicGroupFmt", function() {
    return function(row) { // input是我们传入
        if(row==null) return null;
        return row.topicGroup;
        //return [
        //    row.topicGroup, '(',row.topicGroupEn,')'
        //].join('');
    }
});

app.filter("compareEntityFmt", ['$sce',function($sce) {
    return function(val) { // input是我们传入
        if(val==null) return null;
        var color = "blue";
        if(val.comPreCount>0.01) {
            color = "red"
        }
        //return $sce.trustAsHtml([
        //    '当前值: ',val.valueCount, ' ',
        //    '<font color="',color,'">','较前1天：',val.comPreCount, '% ','</font>',
        //    '较30天均值：', val.comAvgCount,'%'
        //].join(''));

        return [
            '当前值： ',val.valueCount, ' ',
            '较前1天：',val.comPreCount, '% ',
            '较30天均值：', val.comAvgCount,'%'
        ].join('');
    }
}]);
app.filter("compareEntityFmt1", ['$sce',function($sce) {
    return function(val) { // input是我们传入
        if(val==null) return null;
        val = Number(val);
        return numberFmt(val);
    }
}]);
app.filter("compareEntityFmt2", ['$sce',function($sce) {
    return function(val) { // input是我们传入
        if(val==null) return null;
        var flag = '';
        val = Number(val);
        if(val>0) {
            flag = '+';
        }
        return flag+ (val*100).toFixed(2) + "%";
    }
}]);
app.filter("compareEntityFmt3", ['$sce',function($sce) {
    return function(val) { // input是我们传入
        if(val==null) return null;
        var flag = '';
        if(val>0) {
            flag = '+';
        }
        val = Number(val);
        return flag + (val*100).toFixed(2) + "%";
    }
}]);

app.filter("indexCnFmt", function() {
    return function(row) { // input是我们传入
        if(row==null) return null;
        return [
            row.indexCn, '(',row.indexStd, ')'
        ].join('');
    }
});

function topicGroupFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return value;
    //return [
    //    value, '(',row.topicGroupEn,')'
    //].join('');
}

function indexCnFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        value, '(',row.indexEn, ')'
    ].join('');
}


function compareEntityFmt(value, row, index) {
    if (value == null) {
        return "-";
    }

    return [
        '当前值: ',value.valueCount, '<br/>',
        '较前1天值：',value.comPreCount, '% <br/>',
        '较30天均值：', value.comAvgCount,'%'
    ].join('');
}

function seriesFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var items = value.split(',');
    return items.join('<br/>');
}

